import {
	Login
} from "@/api/complete"
import $router from '@/router/index.js'
// 引入插件
import {
	Message
} from 'element-ui';
export default {
	state: {
		user: { //用户数据
			name: "",
			score: null,
		},
		token: ''
	},
	mutations: {
		SET_USER(state, data) {
			state.user = data;
			localStorage.setItem("user", JSON.stringify(data))
		},
		SET_TOKEN(state, data) {
			state.token = data;
			localStorage.setItem("token", data);
		}
	},
	actions: {
		login(context, data) {
			Login(data)
				.then(res => {
					if (res.data.code == 200) {
						console.log(res);
						// 存储用户信息到token,
						context.commit("SET_USER", res.data.user);
						// 存储用户信息
						context.commit("SET_TOKEN", res.data.token);
						Message({
							message: '登陆成功',
							type: 'success',
							duration: 1500
						});
						$router.push("/first"); //跳转至主页面
					} else {
						Message({
							message: '登录失败',
							type: 'warning',
							duration: 1500
						})
					}
				})
				.catch(err => console.error(err))
		}
	}
}
